home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 74 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  97 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.kei.com!wang!news
  3. From: emild@cs.technion.ac.il (Kohn Emil Dan)
  4. Subject: Re: help about structure 
  5. Organization: Technion, Israel Institute of Technology
  6. Date: Sun, 31 Dec 1995 14:51:58 GMT
  7. Message-ID: <Pine.SV4.3.91-heb-2.04.951231163602.21129C-100000@cs.technion.ac.il>
  8. References: <4c3t9q$aob@chleuasme.francenet.fr> 
  9. Sender: news@wang.com
  10.  
  11.  
  12.  
  13. On 30 Dec 1995, jo wrote:
  14.  
  15. > How can I translate from Pascal to C such a structure:
  16. > token=record;
  17. >   begin
  18. >        nature:tokennature;
  19. >        case nature : tokennature of
  20. >               tkinteger: (i:integer);
  21. >               tkreal: (r:double);
  22. >               ...
  23. >   end;
  24. > Can i use "switch" in a structure implementation ?
  25.  
  26. No, you can't.
  27.  
  28.  
  29. A suggestion would be something like:
  30.  
  31.  
  32. struct token
  33. {
  34. tokennature nature;
  35.  
  36. union
  37.   {
  38.     int i;
  39.     double r;
  40.     .
  41.     .
  42.     .
  43.   }value;
  44. };
  45.  
  46.  
  47. And when you _USE_ the structure:
  48.  
  49.  
  50. .
  51. .
  52. .
  53. .
  54. enum {tkinteger,tkreal,....};
  55. .
  56. .
  57. .
  58.  
  59. struct token token;
  60. .
  61. .
  62. .
  63.  
  64. switch (token.nature)
  65. {
  66.  
  67.    case tkinteger:
  68.  
  69.     token.value.i=4;
  70.     .
  71.     .
  72.     .
  73.     break; 
  74.  
  75.    case tkreal:
  76.  
  77.     token.value.d=2.5;
  78.     .
  79.     .
  80.     .
  81.     break;
  82.  
  83.  
  84.   .
  85.   .
  86.   .
  87. }
  88. .
  89. .
  90. .
  91.  
  92. Best regards, and a HAPPY NEW YEAR!!!
  93.  
  94.  
  95.                                 Emil
  96.